'use strict';

const db = uniCloud.database()
const $ = db.command.aggregate
exports.main = async (event, context) => {
	//event为客户端上传的参数
	const { user_id, type } = event
	
	let matchObj = {}
	if (type !== "all") matchObj = {current: true}
	
	let  userinfo = await db.collection("user").doc(user_id).get()
	userinfo = userinfo.data[0]
	// let label = await db.collection('label').get()
	let label = await db.collection("templateLabel").aggregate()
	.addFields({ // 往数据内添加字段
		//  $.in() 
		current: $.in(['$_id', $.ifNull([userinfo.template_ids, []])])
	})
	.match(matchObj)
	.end()
	// console.log('event : ', label)
	
	//返回数据给客户端
	return {
		status: 200,
		msg: '查询成功',
		data:label.data
		}
};
